<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
         <el-row>
            <el-col>
               <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
            </el-col>
         </el-row>
            <el-table :data="catelist" border stripe style="width: 100%">
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="type" label="分类名称"></el-table-column>

                <!-- <el-table-column label="是否有效">
                    <template v-slot="scope">
                        <el-switch
                                v-model="scope.row.status"
                                @change="userStateChanged(scope.row)"
                        ></el-switch>
                    </template>
                </el-table-column> -->
                <el-table-column label="操作">
                    <template v-slot="scope">
                        <!-- 编辑 -->
                        <!-- <el-button
                                type="primary"
                                icon="el-icon-edit"
                                @click="editCate(scope.row.id)"
                                size="mini"
                        ></el-button> -->
                        <!-- 删除分类 -->
                        <el-button
                                type="danger"
                                @click="deleteCate(scope.row.id)"
                                icon="el-icon-delete"
                                size="mini"
                        ></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum"
                    :page-sizes="[1, 2, 5, 10]"
                    :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
            ></el-pagination>
        </el-card>

        <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      @close="addCateDialogClosed"
      width="50%"
    >
      <el-form
        ref="addCateFormRef"
        :model="addCateForm"
        :rules="addCateFormRules"
        label-width="100px"
      >
        <el-form-item label="分类名称：" prop="type">
          <el-input v-model="addCateForm.type"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query:"",
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      catelist: [],
    //   editCateForm: {
    //       id:0,
    //       status:"",
    //       type:""
    //   },
      dialogVisible: false,
      addCateDialogVisible: false,
     // editDialogVisible: false,
      addCateForm: {
        id:0,
        status:"",
        type: ""
      },
      addCateFormRules: {
        type: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      }, 
    };
  },
        methods: {
            async getCateList() {
                const {data} = await this.$http.get("mtype/findTypeByParams", {
                    params: this.queryInfo
                });
                console.log(data)
                this.catelist = data.data.content;
                this.total = data.data.totalElements;
            },
            handleSizeChange(newSize) {
                this.queryInfo.pagesize = newSize;
                this.getCateList();
            },
            handleCurrentChange(newPage) {
                this.queryInfo.pagenum = newPage;
                this.getCateList();
            },
            showAddCateDialog() {
                this.addCateDialogVisible = true;
            },
            addCateDialogClosed() {
                this.$refs.addCateFormRef.resetFields();
            },
            addCate() {
                this.$refs.addCateFormRef.validate(async valid => {
                    if (!valid) return;
                    const {data} = await this.$http.post("mtype/create", this.addCateForm);
                    console.log(data.meta.status)
                    if (data.meta.status !== 200) {
                        return this.$message.error("添加分类失败");
                    }
                    this.$message.success("添加分类成功！");
                    this.addCateDialogVisible = false;
                    this.getCateList();
                });
            },
            // editCate(){
            //      this.editDialogVisible = true;
            // },
            // editDialogClosed() {
            //     this.$refs.editCateFormRef.resetFields();
            // },
            // editCate() {
            //     this.$refs.editCateFormRef.validate(async valid => {
            //         if (!valid) return;
            //         const {data} = await this.$http.post('mtype/update', this.editCateForm);
            //         if (data.meta.status !== 200) {
            //             return this.$message.error("修改信息失败！");
            //         }
            //         this.$message.success("修改成功！");
            //         this.editDialogVisible = false;
            //         this.getCateList();
            //     });
            // },
            deleteCate(id) {
                this.$confirm("此操作将永久删除该分类, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(async () => {
                        const {data} = await this.$http.delete(`mtype/delete?id=${id}`);
                        if (data.meta.status !== 200) {
                            this.$message.error(data.meta.msg);
                        } else {
                            this.$message({
                                type: "success",
                                message: "删除成功!"
                            });
                            this.getCateList();
                        }
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            }
        },
        created() {
            this.getCateList();
        }
    };
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
